package com.atguigu.acl.service.impl;

import com.atguigu.acl.dto.MenuNode;
import com.atguigu.acl.entity.Menu;
import com.atguigu.acl.mapper.MenuMapper;
import com.atguigu.acl.service.MenuService;
import com.atguigu.utils.PageUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author smday
 * @since 2020-08-10
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {


    @Override
    public List<Menu> getMenuList(Long adminId) {
        return baseMapper.getMenuList(adminId);
    }

    @Override
    public int create(Menu menu) {
        updateLevel(menu);
        return baseMapper.insert(menu);
    }

    private void updateLevel(Menu menu) {
        if(menu.getPid() == 0){
            menu.setLevel(0);
        }else {
            //有父级菜单
            Menu parent = baseMapper.selectById(menu.getPid());
            if(parent!=null){
                menu.setLevel(parent.getLevel()+1);
            }else {
                menu.setLevel(0);
            }
        }
    }

    @Override
    public int update(Long id, Menu menu) {
        menu.setId(id);
        updateLevel(menu);
        return baseMapper.updateById(menu);
    }

    @Override
    public Menu getItem(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public int delete(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public Map<String, Object> list(Long parentId, Integer pageSize, Integer pageNum) {
        Page<Menu> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("sort");
        wrapper.eq("pid",parentId);
        baseMapper.selectPage(page,wrapper);
        return PageUtils.convertPageToMap(page);
    }

    @Override
    public List<MenuNode> treeList() {

        //查询出所有的menu
        List<Menu> menuList = baseMapper.selectList(null);
        return menuList.stream().filter(menu -> menu.getPid().equals(0L))
                .map(menu -> convertMenuNode(menu, menuList))
                .collect(Collectors.toList());
    }

    private MenuNode convertMenuNode(Menu menu, List<Menu> menuList) {

        MenuNode menuNode = new MenuNode();
        BeanUtils.copyProperties(menu,menuNode);
        List<MenuNode> children = menuList.stream().filter(subMenu -> subMenu.getPid().equals(menu.getId()))
                .map(subMenu -> convertMenuNode(subMenu, menuList))
                .collect(Collectors.toList());

        menuNode.setChildren(children);
        return menuNode;
    }

    @Override
    public int updateHidden(Long id, Integer hidden) {
        Menu menu = new Menu();
        menu.setId(id);
        menu.setHidden(hidden);
        return baseMapper.updateById(menu);
    }
}
